package com.xmb.wechat.view.wechat.chat;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.Spinner;

import com.blankj.utilcode.util.ToastUtils;
import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.bean.WechatContactBean;
import com.xmb.wechat.definterface.HintListener;
import com.xmb.wechat.definterface.SimpleSpinnerSelectedListener;
import com.xmb.wechat.delegate.GroupCallDelegate;
import com.xmb.wechat.dialog.GroupCallStateDialog;
import com.xmb.wechat.entity.GroupCallEntity;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Author lijiangop
 * @CreateTime 2020/4/14 17:11
 */
public class WechatGroupCallActivity extends BaseActivity {

    @BindView(R2.id.btn_save)
    Button  mBtnSave;
    @BindView(R2.id.spinner_min)
    Spinner mSpinnerMin;
    @BindView(R2.id.spinner_second)
    Spinner mSpinnerSecond;

    private GroupCallDelegate mDelegate;
    private int               mMin, mSec;
    private GroupCallEntity      mGroupTalkEntity;
    private GroupCallStateDialog mGroupTalkStateDialog;

    public WechatGroupCallActivity() {
        super(R.layout.activity_wechat_group_call);
    }

    @Override
    protected void initView() {
        mDelegate = new GroupCallDelegate(this);
        mSpinnerMin.setOnItemSelectedListener(new SimpleSpinnerSelectedListener() {

            @Override
            public void onSelected() {
                String strMin = (String) mSpinnerMin.getSelectedItem();
                mMin = Integer.parseInt(strMin.replace("分", ""));
                checkBtnState();
            }
        });
        mSpinnerSecond.setOnItemSelectedListener(new SimpleSpinnerSelectedListener() {
            @Override
            public void onSelected() {
                String strSec = (String) mSpinnerSecond.getSelectedItem();
                mSec = Integer.parseInt(strSec.replace("秒", ""));
                checkBtnState();
            }
        });
        showExemption(new HintListener() {
            @Override
            public void onConfirm() {
                //do nothing
            }

            @Override
            public void onCancel() {
                WechatGroupCallActivity.this.finish();
            }
        });
    }

    @OnClick({R2.id.btn_save, R2.id.re_add})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.btn_save) {
            String strTime = (mMin < 10 ? "0" + mMin : mMin) + ":" + (mSec < 10 ? "0" + mSec : mSec);
            WechatGroupCallDetailActivity.start(WechatGroupCallActivity.this, mDelegate.getData(), strTime);
        } else if (id == R.id.re_add) {
            if (mDelegate.getData().size() == 4) {
                ToastUtils.showShort("目前暂支持最多4人群聊");
                return;
            }
            showRoleChooseReceiver(this,false,false,null);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_CHOOSE_OTHER_RECEIVE || requestCode == REQUEST_CODE_CHOOSE_NEW_ADD_RECEIVE) {
            if (data == null)
                return;
            WechatContactBean result = (WechatContactBean) data.getSerializableExtra("result");
            if (result == null)
                return;
            showGroupTalkStateDialog(result);
        }
    }

    private void showGroupTalkStateDialog(WechatContactBean result) {
        List<GroupCallEntity> data = mDelegate.getData();
        if (mDelegate.getData().size() > 0) {
            for (GroupCallEntity groupCallEntity : data) {
                if (groupCallEntity.getContactId()==result.getId()){
                    ToastUtils.showShort("同一成员不能重复添加");
                    return;
                }
            }
        }
        //创建群通话实体类
        mGroupTalkEntity = new GroupCallEntity();
        mGroupTalkEntity.setName(result.getName());
        mGroupTalkEntity.setContactId(result.getId());
        mGroupTalkEntity.setAvatar(result.getAvatar());
        mGroupTalkEntity.setAvatarSrc(result.getAvatarSrc());
        //选择通话状态
        if (mGroupTalkStateDialog == null) {
            mGroupTalkStateDialog = new GroupCallStateDialog(WechatGroupCallActivity.this, state -> {
                mGroupTalkEntity.setState(state);
                mDelegate.additem(mGroupTalkEntity);
                checkBtnState();
            });
        }
        mGroupTalkStateDialog.show();
    }

    private void checkBtnState() {
        if (mMin == 0 && mSec == 0) {
            mBtnSave.setEnabled(false);
            return;
        }
        if (mDelegate.getData().size() == 0) {
            mBtnSave.setEnabled(false);
            return;
        }
        mBtnSave.setEnabled(true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mGroupTalkStateDialog != null) {
            mGroupTalkStateDialog.dismiss();
            mGroupTalkStateDialog = null;
        }
    }
}
